home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3655 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple i/o for my class
  5. Date: 30 Jan 1996 08:05 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <30JAN199608051674@erich.triumf.ca>
  9. References: <4ekjql$5jl@nnrp1.news.primenet.com>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4ekjql$5jl@nnrp1.news.primenet.com>, mcoplea@primenet.com (Marty R. Coplea) writes...
  14. >I am having a little trouble with a batting average program.
  15. >It seems to take the input OK, but always returns a '0' for the Batting 
  16. >Average.  Please look at the following code and make any suggestions.  Thank 
  17. >you in advance for any assistance you can offer. (email perfered)
  18.  
  19. .. snip...
  20. >    /* Prompt the user to input the data */
  21. >    printf("Enter the number of at-bats followed by hits:   ");
  22. >    scanf("%f %f", &avar, &hvar);
  23.  
  24. It is often better to get user input with fgets(), then use sscanf()  - it
  25. gives you a better chance to recover from entry errors.
  26. >    /* Convert to integers */
  27. >    Atbatsint = avar;
  28. >    Hitsint = hvar;
  29.  
  30. Why??
  31. >    /* Calculate the Batting Average */
  32. >    Avg = Hitsint/Atbatsint;
  33.  
  34. The division will be done with integer arithmetic, where 
  35. (small number)/(big number) is always  zero.
  36.  
  37. Since Avg is a float, why not leave the Atbats and Hits as floats?  Then you
  38. should get the expected results.
  39.  
  40.  
  41. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  42. Internet: bennett@triumf.ca         | of one another only when one can be
  43. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  44. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  45. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  46.  
  47.  
  48.  
  49.  
  50.